var MainContent = (function() {
	var $container = null;
	var history = new History();

	/*
	 Sample code:
	 
	 //Somewhere else
	 
	 var $container = $("#main_container");
	 MainContent.init($container);
	 
	 (...)
	 //here code to get data and assign it to 'var data'
	 
	 $.each(data.someArray, function(i, object) {
	 	var $template = SomeTemplate.common();
	 	SomeLoader.attachToCommon(data, $template);
	 	
	 	MainContent.append($template);
	 });
	 
	 MainContent.show();
	 
	 */
	
	return {
		init: function(_$container) {
			$container = _$container;
		},
		
		clear: function() {
			var children = $container.children();
			//children.fadeOut(200, function() {
				children.detach();
			//});
		},
		
		prepend: function($object) {
			$object.hide();
			$container.prepend($object);
		},
		
		append: function($object) {
			$object.hide();
			$container.append($object);
		},
		
		save: function() {
			history.add($container.children());
		},
		
		show: function() {
			$container.children().fadeIn();
		},
		
		back: function() {
			MainContent.clear();
			MainContent.append(history.previous());
			MainContent.show();
		},
		
		forward: function() {
			MainContent.clear();
			MainContent.append(history.next());
			MainContent.show();
		}
	};
})();

function History() {
	var pointer = 0;
	var historyArray = [];
	
	this.next = function() {
		if(historyArray.length === 0) {
			return null;
		}
		
		var nextObject = null;
		
		if(pointer === 0) {pointer++;}
		
		if(pointer === historyArray.length) {
			nextObject = historyArray[pointer-1];
		} else {
			nextObject = historyArray[pointer];
			pointer++;
		}
		
		return nextObject;
	};
	
	this.previous = function() {
		if(historyArray.length === 0) {
			return null;
		}
		
		var nextObject = null;
		
		if(pointer === 0) {
			nextObject = historyArray[pointer];
		} else {
			nextObject = historyArray[pointer-1];
			pointer--;
		}
		
		return nextObject;
	};
	
	this.add = function(object) {
		historyArray[pointer] = object;
		
		removeForward();
	};
	
	function removeForward() {
		var sliceStart = (pointer - 10) < 0 ? 0 : (pointer - 10);
		var sliceEnd = pointer+1;
		
		historyArray = historyArray.slice(sliceStart, sliceEnd);
		pointer = historyArray.length;
	};
};